package com.his.controller;


import com.his.commons.core.dto.PageInfo;
import com.his.commons.core.dto.Result;
import com.his.commons.core.exception.HisException;
import com.his.commons.web.exception.ParamException;
import com.his.commons.web.util.ValidatedUtil;
import com.his.controller.form.ModifyRoleForm;
import com.his.controller.form.NewRoleForm;
import com.his.controller.form.PageForm;
import com.his.controller.info.RoleInfo;
import com.his.service.RoleService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 梅津铧
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色接口")
@GlobalTransactional(rollbackFor = Exception.class)
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 角色注册
     * @param newRoleForm  新增角色信息
     * @param bindingResult  参数校验对象
     */
    @ApiOperation("角色注册")
    @PostMapping("/increaseRole")
    public Result IncreaseRole(@RequestBody @Valid NewRoleForm newRoleForm, BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = roleService.increaseRole(newRoleForm);
        if(!b) throw new HisException("角色注册失败",500);
        return Result.getInstance().setCode(200).setMsg("角色注册成功");
    }

    /**
     * 角色更新
     * @param modifyRoleForm  新增角色信息
     * @param bindingResult  参数校验对象
     */
    @ApiOperation("角色更新")
    @PutMapping("/modifyRole")
    public Result ModifyRole(@RequestBody @Valid ModifyRoleForm modifyRoleForm, BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = roleService.modifyRole(modifyRoleForm);
        if(!b) throw new HisException("角色更新失败",500);
        return Result.getInstance().setCode(200).setMsg("角色更新成功");
    }

    /**
     * 获取角色
     * @param pagenum  页码
     * @param pagesize  每页数据量
     * @return  每页的角色信息
     * @throws ParamException
     */
    @ApiOperation("获取角色")
    @GetMapping("/obtainRoles")
    public Result<PageInfo<RoleInfo>> ObtainRoles(@RequestParam(value = "pagenum",required = false) Integer pagenum,
                                              @RequestParam(value = "pagesize",required = false) Integer pagesize) throws HisException {
        PageForm pageForm = new PageForm();
        pageForm.setPagenum(pagenum);pageForm.setPagesize(pagesize);

        PageInfo<RoleInfo> roleInfos = roleService.obtainRoles(pageForm);
        if(null == roleInfos) throw new HisException("获取角色信息失败",500);
        return Result.getInstancePageInfo(RoleInfo.class).setData(roleInfos);
    }

    /**
     * 获取所有角色
     * @return  全部角色信息
     * @throws ParamException
     */
    @ApiOperation("获取所有角色")
    @GetMapping("/obtainAllRoles")
    public Result<List<RoleInfo>> ObtainAllRoles() throws HisException {

        List<RoleInfo> roleInfos = roleService.obtainAllRole();
        if(roleInfos.size()==0) throw new HisException("当前无角色可查",500);
        return Result.getInstanceList(RoleInfo.class).setData(roleInfos);
    }

    /**
     * 注销角色
     * @param id  角色id
     */
    @ApiOperation("注销角色")
    @DeleteMapping("/logoutRole/{id}")
    public Result LogoutRole(@PathVariable("id") Integer id) throws HisException {

        boolean b =  roleService.logoutRole(id);
        if(!b) throw new HisException("角色注销失败",500);
        return Result.getInstance().setCode(200).setMsg("角色注销成功");
    }

    /**
     * 根据角色名获取角色信息
     * @param condition 角色名
     * @return 角色信息
     */
    @ApiOperation("根据角色名获取角色信息")
    @GetMapping("/obtainOneRole")
    public Result<List<RoleInfo>> ObtainOneRole(@PathVariable("condition") String condition) throws HisException {

        List<RoleInfo> roleInfo = roleService.obtainOneRole(condition);
        if(null == roleInfo) throw new HisException("",500);
        return Result.getInstanceList(RoleInfo.class).setData(roleInfo);
    }
}

